﻿Imports System.Data.OleDb
Imports System.Xml
Imports System.ServiceModel.Syndication

Public Class rss
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.ContentType = "application/rss+xml"

        Dim myFeed As New SyndicationFeed

        myFeed.Title = TextSyndicationContent.CreatePlaintextContent("Latest Motorcycle Modifications")
        myFeed.Description = TextSyndicationContent.CreatePlaintextContent("This shows all the latest motorcycle modifications which users have uploaded")
        myFeed.Links.Add(SyndicationLink.CreateSelfLink(New Uri(myLink("modificationList.aspx"))))
        myFeed.Language = "en-us"


        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("MotorcyclesConn").ConnectionString)
        oleDbConn.Open()
        Dim cmdQuery As String = "SELECT * FROM MyTable"
        Dim dbComm = New OleDbCommand(cmdQuery, oleDbConn)
        Dim dbread = dbComm.ExecuteReader()


        Dim myItems As New List(Of SyndicationItem)
        While dbread.Read()
            Dim sItem As New SyndicationItem
            sItem.Title = mkRssTxt(dbread("Make") & (" ") & dbread("Model") & (" - ") & dbread("Category"))
            sItem.Summary = mkRssTxt(dbread("Description") & ("<br/>") & dbread("DetailedDescription"))

            myItems.Add(sItem)
        End While

        myFeed.Items = myItems

        Dim feedWriter As XmlWriter = XmlWriter.Create(Response.OutputStream)

        Dim rssFormatter = New Rss20FeedFormatter(myFeed)

        rssFormatter.WriteTo(feedWriter)

        feedWriter.Close()

    End Sub

    Private Function myLink(ByVal urlStr As String)
        Return Request.Url.GetLeftPart(UriPartial.Authority) & ResolveUrl(urlStr)
    End Function

    Private Function mkRssTxt(ByVal someString As String) As TextSyndicationContent
        Return TextSyndicationContent.CreatePlaintextContent(someString)
    End Function

    Private Function mkRssLink(ByVal someLocation As String) As SyndicationLink
        Return SyndicationLink.CreateAlternateLink(New Uri(someLocation))
    End Function

End Class